package com.hanson.controller;

import com.hanson.common.bean.HaiBaiBean;
import com.hanson.common.bean.ResultMessage;
import com.hanson.common.bean.WeiXinConfig;
import com.hanson.common.util.BusinessUtil;
import com.hanson.common.util.ImageUtil;
import com.hanson.entity.GoodsBean;
import com.hanson.entity.UserBean;
import com.hanson.service.GoodsService;
import com.hanson.service.UserService;
import com.hanson.service.UserShareService;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/web/goods")
public class WebGoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserShareService userShareService;
    @Autowired
    private WeiXinConfig weiXinConfig;
    @Value("${oss.local}")
    private String isLocal;
    @Value("${share.path}")
    private String sharePath;

    @RequestMapping(value = "/getShareImgs/{goodsId}/{type}", method = RequestMethod.GET)
    public ResultMessage add(@PathVariable String goodsId, @PathVariable String type, @CookieValue("openId") String openId) throws Exception {
        long time = System.currentTimeMillis();
        List<String> returnList = userShareService.getShareImgs(openId, goodsId, null, null);
        if (CollectionUtils.isEmpty(returnList)) {
            HaiBaiBean haiBaiBean = new HaiBaiBean();
            GoodsBean goodsBean = goodsService.selectById(goodsId);
            if (null != goodsBean) {
                haiBaiBean.setGoodsId(goodsId);
                haiBaiBean.setGoodsName(goodsBean.getGoodsName());
                haiBaiBean.setGoodsImage(goodsBean.getImage());
                haiBaiBean.setPrice(BusinessUtil.getWebAmount(goodsBean.getPrice()));
            }
            UserBean userBean = userService.selectByOpenId(openId);
            if (null != userBean) {
                haiBaiBean.setUserName(userBean.getNickName());
                haiBaiBean.setUserImage(userBean.getHeadImg());
            }
            BufferedImage bufferedImage = BusinessUtil.generateQRCode(String.format("%sweb/shareDetail/%s/%s/%s", weiXinConfig.getDomian(), goodsId, openId, type), 300, 300);
            haiBaiBean.setQrImage(bufferedImage);

            returnList = new ArrayList();
            returnList.add(imageToUrl(ImageUtil.createImg1(haiBaiBean)));
            returnList.add(imageToUrl(ImageUtil.createImg2(haiBaiBean)));
            returnList.add(imageToUrl(ImageUtil.createImg3(haiBaiBean)));
            userShareService.batchInsert(openId, goodsId, null, null, returnList);
            long time2 = System.currentTimeMillis();
            log.info("share haibai cost time :" + (time2 - time));
        }

        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setData(returnList);
        resultMessage.setMsg("获取分享海报成功！");
        return resultMessage;
    }

    private String imageToUrl(BufferedImage img) throws Exception {
        String fileName = UUID.randomUUID() + ".jpg";
        FileOutputStream fos = new FileOutputStream(sharePath + fileName);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
        encoder.encode(img);
        bos.close();
        fos.close();
        return weiXinConfig.getDomian() + "/web/share/" + fileName;
    }
}
